package com.xwltz.main;

import com.xwltz.entity.*;
import com.xwltz.repository.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RunMain2 {
    public static void main(String[] args) {
        InputStream inputStream = RunMain2.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        AccountRepository accountRepository = sqlSession.getMapper(AccountRepository.class);
//        List<Account> list = accountRepository.findAll();
//        for (Account account : list) {
//            System.out.println(account);
//        }
//
//        Account account = accountRepository.findById(3l);
//        System.out.println(account);
//
//        account.setUsername("李四");
//        account.setPassword("123456");
//        account.setAge(16);
//
//        accountRepository.update(account);
//
//        int result = accountRepository.deleteById(3L);
//        System.out.println(result);
//
//        sqlSession.commit();

//        Account account = accountRepository.findByNameAndAge("com.xwltz", 22);
//        System.out.println(account);

        Account account = new Account();
//        account.setId(1l);
//        account.setUsername("com.xwltz");
//        account.setPassword("123456");
        account.setAge(22);
        Account accountModel = accountRepository.findByAccount(account);
        System.out.println(accountModel);

//        StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
//        Student student = studentRepository.findById(2);
//        System.out.println(student);

//        ClassesRepository classesRepository = sqlSession.getMapper(ClassesRepository.class);
//        Classes classes = classesRepository.findById(1);
//        System.out.println(classes);

//        CustomerRepository customerRepository = sqlSession.getMapper(CustomerRepository.class);
//        Customer customer = customerRepository.findById(1);
//        System.out.println(customer);

//        GoodsRepository goodsRepository = sqlSession.getMapper(GoodsRepository.class);
//        Goods goods = goodsRepository.findById(1);
//        System.out.println(goods);
//
        sqlSession.close();

//        List<String> waring = new ArrayList<String>();
//        boolean overwrite = true;
//        String config = "/generator-config.xml";
//        File configFile = new File(RunMain2.class.getResource(config).getFile());
//        ConfigurationParser configurationParser = new ConfigurationParser(waring);
//        Configuration configuration = null;
//        try {
//            configuration = configurationParser.parseConfiguration(configFile);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (XMLParserException e) {
//            e.printStackTrace();
//        }
//        DefaultShellCallback callback = new DefaultShellCallback(true);
//        MyBatisGenerator myBatisGenerator = null;
//        try {
//            myBatisGenerator = new MyBatisGenerator(configuration, callback, waring);
//        } catch (InvalidConfigurationException e) {
//            e.printStackTrace();
//        }
//        try {
//            myBatisGenerator.generate(null);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
